package com.ali.demo.unicom.ons;

import java.util.Properties;

import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.ONSFactory;
import com.aliyun.openservices.ons.api.Producer;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.aliyun.openservices.ons.api.SendResult;

public class ONSProducerBean {

    private String producerId;
    private String accessKey;
    private String secretKey;

    private Producer producer;

    public String getProducerId() {
	return producerId;
    }

    public void setProducerId(String producerId) {
	this.producerId = producerId;
    }

    public String getAccessKey() {
	return accessKey;
    }

    public void setAccessKey(String accessKey) {
	this.accessKey = accessKey;
    }

    public String getSecretKey() {
	return secretKey;
    }

    public void setSecretKey(String secretKey) {
	this.secretKey = secretKey;
    }

    public Producer getProducer() {
	return producer;
    }

    public void init() {
	Properties properties = new Properties();
	properties.put(PropertyKeyConst.ProducerId, producerId);
	properties.put(PropertyKeyConst.AccessKey, accessKey);
	properties.put(PropertyKeyConst.SecretKey, secretKey);
	producer = ONSFactory.createProducer(properties);
	producer.start();
    }

    public SendResult send(Message message) {
	return producer.send(message);
    }

    public void close() {
	if (producer != null) {
	    producer.shutdown();
	}
    }
}
